<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;

class Tool extends Admin
{
    protected $model = false;
    public function add()
    {
        return view();
    }
    //mvc生成
    public function add_ajax()
    {
        $post = param();
        $view_name = lcfirst($post['controller']);
        //首字母大写
        if(strstr($post['controller'],'_')){
            $arr = explode('_',$post['controller']);
            $post['controller'] = '';
            foreach ($arr as $v){
                $post['controller'] .= ucfirst($v);
            }
        }else{
            $post['controller'] = ucfirst($post['controller']);
        }
        if(!is_dir(WWW.'\app\\' . $post['modular'] . '\controller')){
            mkdir(WWW.'\app\\' . $post['modular'] . '\controller',0777,true);
        }
        //生成控制器
        $controller = "<?php 
declare (strict_types = 1);
namespace app\\".$post['modular']."\controller;
use app\common\controller\\".ucfirst($post['modular']).";
use think\Request;
class " . $post['controller'] . " extends " . ucfirst($post['modular'])
        ;
        if($post['modular'] == 'admin'){
            $controller .= "
{
    public function initialize()
    {
        \$this->menu_input = [
            'all' => [
            ],
            'add' => [
            ],
            'upload' => [
            ],
        ];
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    public function table_mage(&\$data = [])
    {
    
        \$data = [
            ['field' => 'create_time', 'title' => '添加时间'],
            ['field' => 'update_time', 'title' => '修改时间'],
            ['title' => '操作', 'templet' => '#list_setting', 'width' => '7%']
        ];
        return parent::table_mage(\$data); // TODO: Change the autogenerated stub
    }
}";
        }else{
            $controller .= '
{

}';
        }
        if(!file_exists(WWW.'\app\\' . $post['modular'] . '\controller\\'.$post['controller'].'.php')){
            file_put_contents(WWW.'\app\\' . $post['modular'] . '\controller\\'.$post['controller'].'.php',$controller);
        }else{
            return msgerror('控制器已存在，不可重复生成！');
        }
        if($post['model'] == 1){
            //模型生成
            $model = "<?php
declare (strict_types = 1);

namespace app\\".$post['modular']."\model;

use app\common\model\\".ucfirst($post['modular']).";
use think\Model;

/**
 * @mixin think\Model
 */
class " . $post['controller'] . " extends " . ucfirst($post['modular']) .
                "\n{

}";
            if(!is_dir(WWW.'\app\\' . $post['modular'] . '\model')){
                mkdir(WWW.'\app\\' . $post['modular'] . '\model',0777,true);
            }
            if(!file_exists(WWW.'\app\\' . $post['modular'] . '\model\\'.$post['controller'].'.php')){
                file_put_contents(WWW.'\app\\' . $post['modular'] . '\model\\'.$post['controller'].'.php',$model);
            }else{
                return msgerror('模型已存在，不可重复生成。控制器已生成！');
            }
        }
        if($post['view'] == 1 && $post['modular'] == 'admin'){
            //视图生成
            $view_add = "{extend name=\"./addmb\" /}";
            $view_index = "{extend name=\"./indexmb\" /}";
            $view_upload = "{extend name=\"./uploadmb\" /}";
            //视图生成

            if(!is_dir(WWW.'\view\admin\\'.$view_name)){
                mkdir(WWW.'\view\admin\\'.$view_name);
            }
            if(!file_exists(WWW.'\view\admin\\'.$view_name.'\add.html') && !file_exists(WWW.'\view\admin\\'.$view_name.'\index.html') && !file_exists(WWW.'\view\admin\\'.$view_name.'\upload.html')){
                file_put_contents(WWW.'\view\admin\\'.$view_name.'\add.html',$view_add);
                file_put_contents(WWW.'\view\admin\\'.$view_name.'\index.html',$view_index);
                file_put_contents(WWW.'\view\admin\\'.$view_name.'\upload.html',$view_upload);
            }else{
                if($post['model'] == 1){
                    return msgerror('视图文件已存在，创建失败。控制器、模型已生成！');
                }else{
                    return msgerror('视图文件已存在，创建失败。控制器已生成！');
                }
            }
        }
        return datasuccess();
    }
}
